﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntitiesTM;
using Business;
using System.Data;

namespace TimeWork.time.admin
{
    public partial class ViewActivity : System.Web.UI.Page
    {
        private string getParam()
        {
            if (Request.QueryString["aid"] != null)
            {
                return Request.QueryString["aid"].ToString();
            }
            else
            {
                return "0";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadContent();
            }
        }

        private void LoadContent()
        {
            string vid = getParam();
            if (!vid.Equals("0"))
            {
                DataTable activity = BuActivity.selActivityByID2(Int32.Parse(vid), Session["companyid"].ToString());
                lblProjectName.Text = activity.Rows[0]["ProjectName"].ToString();
                txtActivityName.Text = activity.Rows[0]["ActivityName"].ToString();
                txtActivityCode.Text = activity.Rows[0]["ActivityCode"].ToString();
                txtEstimateTime.Text = activity.Rows[0]["ActivityEstimateTime"].ToString();
                lblID.Text = activity.Rows[0]["ActivityID"].ToString();
                lblPID.Text = activity.Rows[0]["ActivityProjectID"].ToString();

                //ddlTypeTime.DataSource = BuProject.selAllTypeTime();
                //ddlTypeTime.DataTextField = "TypeTimeName";
                //ddlTypeTime.DataValueField = "TypeTimeID";
                //ddlTypeTime.DataBind();
                //ddlTypeTime.SelectedValue = activity.Rows[0]["TypeTimeID"].ToString();

                DataTable tmp = BuActivity.selReportTimeActivityPerUser(Int32.Parse(vid), Session["companyid"].ToString());
                DataTable tmp1 = BuActivity.selReportTimeActivity(Int32.Parse(vid), Session["companyid"].ToString());
                if (tmp.Rows.Count > 0)
                {
                    gvPerUser.DataSource = tmp;
                    gvPerUser.DataBind();
                }
                else
                {
                    pnlProjectMember.Visible = false;
                }

                if (tmp1.Rows.Count > 0)
                {
                    gvTime.DataSource = tmp1;
                    gvTime.DataBind();
                }
                else
                {
                    pnlTime.Visible = false;
                }

            }
            else
            {
                Response.Redirect("~/Dashboard.aspx");
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtActivityName.Text.Equals("") && txtEstimateTime.Text.Equals(""))
            {
                lblError.Text = "Tên công việc hoặc thời gian dự kiến không được để trống!";
            }
            else
            {
                try
                {
                    EnActivity activity = new EnActivity();
                    activity.activityName = txtActivityName.Text;
                    activity.activityEstimateTime = Int32.Parse(txtEstimateTime.Text);
                    //activity.activityTypeTimeID = Int32.Parse(ddlTypeTime.SelectedValue);
                    activity.activityID = Int32.Parse(lblID.Text);
                    activity.activityCode = txtActivityCode.Text;
                    BuActivity.updActivity2(activity, Session["companyid"].ToString());

                    Response.Redirect("~/time/admin/ViewProject.aspx?pid=" + lblPID.Text);
                }
                catch
                {
                    lblError.Text = "Thời gian dự kiến phải là số nguyên >= 0";
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            BuActivity.delActivity(Int32.Parse(getParam()), Session["companyid"].ToString());
            Response.Redirect("~/time/admin/ViewProject.aspx?pid=" + lblPID.Text);
        }

        private int totalHour = 0;
        private int totalMinute = 0;
        protected void gvTime_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                totalHour += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SAReportTimeHour"));
                totalMinute += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SAReportTimeMinute"));
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                if (totalMinute >= 60)
                {
                    int addHour = totalMinute / 60;
                    totalMinute -= 60 * addHour;
                    totalHour += addHour;
                }
                Label lblSumHour = (Label)e.Row.FindControl("lblSumHour");
                lblSumHour.Text = totalHour.ToString()+"h";
                Label lblSumMinute = (Label)e.Row.FindControl("lblSumMinute");
                lblSumMinute.Text = totalMinute.ToString()+"p";

            }
        }
    }
}